package com.dream.inspect.modules.filter;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dream.inspect.modules.sys.common.CORSInterceptor;

public class KYFilter implements Filter {
	protected FilterConfig filterConfig = null;
	
	@Resource
    private CORSInterceptor cors;
	@Override
    public void destroy() {
        this.filterConfig = null;
    }

    /**
     * (对用户登陆做session check)
     * 
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
     *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
    	HttpServletRequest hsrq = (HttpServletRequest) request;
        HttpServletResponse hsrp = (HttpServletResponse) response;
        cors.preHandle(hsrq, hsrp, new Object());
        if(hsrq.getMethod().equals("OPTIONS")){
            chain.doFilter(request, response);
        }else{
        chain.doFilter(request, response);
        }
    }

    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

}
